<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到货地址" prop="addr">
        <el-input
          v-model="queryParams.addr"
          placeholder="请输入到货地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['buyers:orderDetailInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderDetailInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单主键" align="center" prop="orderId" />
      <el-table-column label="订单简介" align="center" prop="orderDesc" />
      <el-table-column label="订单状态" align="center" prop="status" >
          <template slot-scope="scope">
              <span >{{ getStatus(scope.row.status) }} </span>
          </template>
        </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="reasonHandle(scope.row)"
          >退货</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="getHandle(scope.row)"
          >确认收货</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="comTitle" :visible.sync="comOpen" width="500px" append-to-body>
      <el-form ref="comForm" :model="comForm" :rules="comRules" label-width="80px">
        <el-form-item label="商品id" prop="goodsId">
          <el-input v-text="comForm.goodsId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="评价内容" prop="comments">
          <el-input v-model="comForm.comments" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
        <div>
            <input type="radio" value="1" v-model="comForm.status">好评
            <input type="radio" value="0" v-model="comForm.status">差评
        </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="comSubmitForm">确 定</el-button>
        <el-button @click="comCancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title" :visible.sync="reOrderOpen" width="500px" append-to-body>
      <el-form ref="form" :model="reForm" label-width="80px">
        <el-form-item label="订单编号" prop="orderId">
          <el-input v-text="reForm.orderId" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="退货原因" prop="reason">
          <el-input v-model="reForm.reason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="makeReturnOrder()">确 定</el-button>
        <el-button @click="returnCancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-text="form.orderNo"  />
        </el-form-item>
        <el-form-item label="订单简介" prop="orderDesc">
          <el-input v-text="form.orderDesc" type="textarea"  />
        </el-form-item>
        <el-form-item label="状态" prop="purchaseId">
          <template slot-scope="scope">
              <span >{{ getStatus(form.status) }} </span>
          </template>
        </el-form-item>
        <el-form-item label="到货地址" prop="addr">
          <el-input v-text="form.addr" />
        </el-form-item>
        <el-divider content-position="center">订单涉及商品信息</el-divider>
        <el-table :data="tGoodsInordersList" :row-class-name="rowTGoodsInordersIndex"  ref="tGoodsInorders">
          <el-table-column label="商品名" align="center" prop="goodsName"/>
          <el-table-column label="商品订购个数" prop="amount" width="150">
            <template slot-scope="scope">
              <el-input v-text="scope.row.amount"  />
            </template>
          </el-table-column>
          <el-table-column>
            <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="comhandleUpdate(scope.row)">评论</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </el-dialog>
  </div>
</template>



<script>
import { listOrderDetailInfo, getOrderDetailInfo, delOrderDetailInfo, addOrderDetailInfo, updateOrderDetailInfo } from "@/api/buyers/orderDetailInfo";

import { updateOrders} from "@/api/supplier/orders";
import { addComments, selectByGoodAndUser, listComments, updateComments } from "@/api/buyers/comments";

import { addOrders} from "@/api/system/orders";

import { getGoodsManage} from "@/api/buyers/goodsManage";

export default {
  name: "OrderDetailInfo",
  data() {
    return {

          // 表单参数
          comForm: {},
          // 表单校验
          comRules: {
            goodsId: [
              { required: true, message: "商品id不能为空", trigger: "blur" }
            ],
          },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTGoodsInorders: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      orderDetailInfoList: [],
      // 订单商品信息表格数据
      tGoodsInordersList: [],
      // 弹出层标题
      title: "",
      comTitle: "",
      // 是否显示弹出层
      open: false,
      comOpen: false,
      reOrderOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        orderDesc: null,
        purchaseId: null,
        addr: null,
        status: null
      },
      // 表单参数
      form: {},
      lasetRow:'',
      reForm: {},
      // 表单校验
      rules: {
        purchaseId: [
          { required: true, message: "订单采购商id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      listOrderDetailInfo(this.queryParams).then(response => {
        this.orderDetailInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        orderNo: null,
        orderDesc: null,
        purchaseId: null,
        addr: null,
        createTime: null,
        updateTime: null,
        updateBy: null,
        status: null
      };
      this.tGoodsInordersList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrderDetailInfo(orderId).then(async response => {
        this.form = response.data;
        this.tGoodsInordersList = response.data.tgoodsInordersList;
        for(var  i=0;i<this.tGoodsInordersList.length;i++){
          let result = await getGoodsManage(this.tGoodsInordersList[i].goodsId);
          this.tGoodsInordersList[i].goodsName=result.data.goodsName;
        }
        this.open = true;
        this.title = "修改订单信息";
      });
    },
	/** 订单商品信息序号 */
    rowTGoodsInordersIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 订单商品信息添加按钮操作 */
    handleAddTGoodsInorders() {
      let obj = {};
      obj.amount = "";
      this.tGoodsInordersList.push(obj);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('buyers/orderDetailInfo/export', {
        ...this.queryParams
      }, `orderDetailInfo_${new Date().getTime()}.xlsx`)
    },
    comhandleUpdate(row) {
      if(this.form.status!='5'){
       this.$modal.msgSuccess("订单未完成，不能评价");
       return;
      }
      this.lasetRow=row;
      this.comForm = {
        commentId: null,
        goodsId: row.goodsId,
        userId: null,
        status: "1",
        comments: null,
        createTime: null,
        updateTime: null
      };
      let queryParams= {
              pageNum: 1,
              pageSize: 10,
              goodsId: row.goodsId,
              status: null,
              comments: null,
      }
      this.resetForm("comForm");
      selectByGoodAndUser(queryParams).then(response => {
        if(response.data.commentId!=null)this.comForm = response.data;
        this.comOpen = true;
        this.open = false;
        this.comTitle = "修改商品评论信息";
      });
    },
    comCancel() {
        this.comOpen = false;
        this.open = true;
        this.comForm = {
          commentId: null,
          goodsId: null,
          userId: null,
          status: null,
          comments: null,
          createTime: null,
          updateTime: null
        };
        this.resetForm("comForm");
    },
           comSubmitForm() {
              this.$refs["comForm"].validate(valid => {
                if (valid) {

                  if (this.comForm.commentId != null) {
                    updateComments(this.comForm).then(response => {
                    this.$modal.msgSuccess("修改成功");
                  });
                  }
                  else {
                    addComments(this.comForm).then(response => {
                      this.$modal.msgSuccess("新增成功");
                      this.comhandleUpdate(this.lasetRow)
                    });
                  }
                }
              });
            },
    makeReturnOrder(){
      addOrders(this.reForm).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.reOrderOpen = false;
        this.getList();
      });
    },
    reasonHandle(row){

      this.reForm = {
        returnedOrdersId: null,//后端填入
        orderId: row.orderId,
        reason: "",
        refund: null,//后端填入
        createTime: null,//后端
        updateTime: null,//后端
        updateBy: null,//后端
        status: null//后端
      };
      this.reOrderOpen = true;
    },
    getHandle(row){
      if(parseInt(row.status) < 4){
        this.$modal.msgSuccess("系统显示该订单未到达目的地，无法确认收货");
        return
      }
      if(parseInt(row.status) > 4){
        this.$modal.msgSuccess("系统显示该订单已收货，无法重复收货");
        return
      }
      row.status='5'
      updateOrders(row).then(response => {
        this.$modal.msgSuccess("确认收货成功");
      });
    },
    getStatus(status)
    {
      console.log(status)
      let i=parseInt(status)
      if(i==1)return "待支付";
      if(i==2)return "待发货";
      if(i==3)return "待到货";
      if(i==4)return "待签收";
      if(i==5)return "订单结束";
      return "订单中止";
    },
    returnCancel() {
        this.reOrderOpen = false;
        this.getList();
    },
  }
};
</script>
